
var layer;
$(function(){
	getLeftMenu();
	getPrinter();
	$(".rePing").click(function(){
		console.log(11);
		setCookie("shop_id",0);
		_ajax_post_token("../crus/getUserById",{id:getCookie("uid"),userType:getCookie("adm")},function(res){
			console.log(res);
			if(res.resultCode == 1000){
				setCookie("acc",res.returnObject.userAccount);
				parent.window.location.reload();
				$(".rePing").hide();
				setCookie("zi","0");
				setCookie("shopTitle","城跑 - 后台管理");
			}
		})
	})
	aa()
})
function aa(){
	_ajax_post_token("../crus/getUserById",{id:getCookie("uid"),userType:getCookie("adm")},function(res){
		console.log(res);
		if(res.resultCode == 1000){
			setCookie("acc",res.returnObject.userAccount);
			if(getCookie("adm") == 0){
				$(".rePing").hide();
			}
			setCookie("zi","0");
			setCookie("shopTitle",getCookie("adm") == 1 ? "城跑 - 后台管理":res.returnObject.shopTitle);
			$(".logo a").text(getCookie("adm") == 1 ? "城跑 - 后台管理":res.returnObject.shopTitle);
		}
	})
}

function cacheCloseTopTab(){
   return $('.layui-tab-title li.layui-this i');
}
function closeTopTab(_this){
	$(_this).click();
}
function clickMenuOnChild(_path){
	console.log(_path);
	if(_path.split("?").length>0){
		_path = _path.split("?")[0];
		$("#nav a[_href='"+_path+"']").parent().click();
	}else{
		$("#nav a[_href='"+_path+"']").parent().click();
	}
}
function clickMenuOnChildParam(_path){
	console.log(_path);
	$("#nav a[_href='"+_path+"']").parent().click();
}

var spid ;
function getPrinter(){
	_ajax_post_token('../print/getShopPrintListByOn',{shopId:spid},function(res){
	});
}
function getLeftMenu(){
	spid = getCookie("shop_id");
/*	if(getCookie("shop_id") == 0){
		$("#f0").attr('src','data~data-statistics.html');
	}*/
	console.log(timeRange('11:00','4:22'));
	$(".rePing").show();
	if(!getCookie("timestamp")){
		setCookie("timestamp",new Date().getTime());
	}
    var shopTitle = getCookie('shopTitle');
	console.log(shopTitle);
	if(shopTitle){
		$(".logo a").text(shopTitle);
	}
    if(	getCookie("zi") == 1 ){
    	$(".rePing").show();
    	console.log(11);
    }else{
    	$(".rePing").hide();
    }
//	setCookie("shop_id","18ydg");
	var acc = getCookie("acc");
	$("#userAcc").text(acc);
	_ajax_post_token('../crus/getUsRule',{userAccount:acc},function(res){
		console.log(res);
		var html = '';
		var list = res.returnObject.list;
		/*html+='<li>'
			+' <a href="javascript:;" id="indexhome">                        '
//			+'        <i class="iconfont">&#xe6b8;</i>              '
			+'        <i class="layui-icon"></i>             '
			+'        <cite style="margin-left: 22px;">首页</cite> '
			+'    </a>'
		+'    </li>';*/
		
		if(list){
			$.each(list,function(i,v,arr){
				if(!v.pId || v.pId == 74){
					if(v.isShow == 0){
						html+='<li class="hide">';
					}else{
						html+='<li>';
					}
					
					 //查找子集
					var rhtml = '';
					 $.each(list,function(ii,vv,arr2){
						 if(v.id === vv.pId){
							 if(vv.isShow == 0){
									rhtml+='<li class="hide">';
								}else{
									rhtml+='<li>';
								}
							 rhtml+='            <a _href="'+vv.url+'">               '
								+'                <i class="iconfont">'+vv.icon+'</i>      '
								+'                <cite>'+vv.title+'</cite> <span></span>                '
								+'            </a>                                      '
								+'        </li >                                        '
								+'   ';
						 }
					 })
					 html+=' <a href="javascript:;">                        '
						+'        <i class="iconfont">'+v.icon+'</i>              '
						+'        <cite>'+v.title+'</cite> <span></span>                        '
						+'        <i class="iconfont nav_right">&#xe697;</i>    '
						+'    </a>    <ul class="sub-menu">      ';
					 
					 html+=rhtml +"  </ul></li > ";
					 $("#nav").html(html);
				}
			});
		}
		//getEnterIn();
		//默认加载第一个
		
		setTimeout(function(){
			console.log(113);
			$('#nav').find('li').eq(0).find('ul').eq(0).find('li').eq(0).click();
		},1000);
	});
}

function showLodopInfo(obj){
	var int = getCookie('dialg_show');
	if(int&&int ==1){
		return;
	}else{
		setCookie('dialg_show',1);
	}
	obj = '本站USB打印机需安装打印插件，如果您的打印机为USB连接方式，请务必下载安装<br>如果您的打印机为WiFi或蓝牙连接，请点击下方忽略[按钮]<br><br>（若此前已安装过，可<a href="CLodop.protocol:setup" target="_self">点这里直接再次启动</a>）';
        //示范一个公告层
        layer.open({
          type: 1
          ,title: false //不显示标题栏
          ,closeBtn: false
          ,area: '300px;'
          ,shade: 0.8
          ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
          ,btn: ['一劳永逸', '忽略']
          ,btnAlign: 'c'
          ,moveType: 1 //拖拽模式，0或者1
//          ,content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">你知道吗？亲！<br>layer ≠ layui<br><br>layer只是作为Layui的一个弹层模块，由于其用户基数较大，所以常常会有人以为layui是layerui<br><br>layer虽然已被 Layui 收编为内置的弹层模块，但仍然会作为一个独立组件全力维护、升级。<br><br>我们此后的征途是星辰大海 ^_^</div>'
          ,content:'<div style="padding: 50px;     font-size: 17px; line-height: 22px; background-color: #ccc; color: red; font-weight: 300;">'+ obj +'</div>'
          ,success: function(layero){
            var btn = layero.find('.layui-layer-btn');
            btn.find('.layui-layer-btn0').attr({
              href: 'http://www.lodop.net/download/CLodop_Setup_for_Win32NT_https_3.060Extend.zip'
              ,target: '_blank'
            });
          }
        });
}

window.onbeforeunload = onbeforeunload_handler;   
function onbeforeunload_handler()
{   
     removeCookie("_p1_t_"+spid);
}   

var tit = setInterval(getPOrder,5000);
//setTimeout(getPOrder,15000);
function getPOrder(){
	
	if(!LODOP){
		LODOP = getLodop();
		if(!LODOP){
			clearInterval(tit);
			return;
		}
	}
	if(getCookie("adm")==1){
		clearInterval(tit);
		return;
	}
	
	//判断用户的打印机类型是否为usb 如果不是则取消调用本地打印机
	var pr = getCookie("pr_type");
	if(pr!=3){
		clearInterval(tit);
		return;
	}
	//如果是usb的话, 判断当前电脑是否有对应的驱动
//	var defPr = getPrinterName(-1);
	//判断打印机个数
	var defPr2 = getPrinterCount();
	if(defPr2==0){
		layer.alert("无法获取本计算机打印机信息,可能无法正常打印订单任务,请检查打印机是否连接正确或联系客服电话:"+getCookie("kf"));
		clearInterval(tit);
		return;
	}
	/*if(defPr.indexOf('Printer AB')<0 && defPr.indexOf('GP-58')<0){
	}else{
		layer.alert("获取本计算机默认打印机异常，可能无法正常打印订单任务，<br>请检查默认打印机是否为本站支持的小票打印机或联系客服电话："+getCookie("kf"));
		clearInterval(tit);
		return;
	}*/
	
	var t1 = getCookie("_p1_t_"+spid);
	var tt = new Date().getTime();
//	console.log(tt+"-"+t1 +"="+ (tt-t1));
	if(t1){
		if(tt-t1 < 4000){
//			console.log("其他定时器未停止");
			return;
		}
	}
	setCookie("_p1_t_"+spid,tt);
	_ajax_post_token('../p/getPrinterOrder',{shopId:spid},function(res){
		if(res.returnObject){
			 $.each(res.returnObject.list,function(i,v,arr){
				 console.log(v);
				 setOrderInfo(v);
			 })
		}
	});
}
function setOrderInfo(o){
	console.log(o);
	var stat = "";
	if(o.status == 1 ){
		stat="未支付";
	}else{
		stat="已支付";
	}
	var ht = "";
	ht = '<p class="sys" >#<span>'+o.sort+'</span>&nbsp;城跑外卖</p>'
				+'<p class="line">-----------------------------------------------------------</p>'
				+'<p class="line-2">---------------------------------------------------------</p>'
				+'<div  class="center">'+o.shopTitle+'</div>'
				+'<div  class="center x25">'+(o.payType ==1 ?'微信支付(':'现金结算(')+stat+')</div>'
				+'<div  class="center x12">订单号:'+o.orderNo+'</div>'
				+'<div  class="center x12">下单时间:'+formatDate(o.createTime)+'</div>';
				if(o.type==2){
					ht+='<div>'
						+'<p class="line">----------------------------------------------------------</p>'
						+'<p class="line-2">-----------------------------------------------------------</p>'
						+'<div  class="center x25">'+o.userAddress+'&nbsp;</div>'
						+'</div>';
				}
				ht+='<p class="line">-------------------------------------------------------------</p>'
				+'<p class="line-2">---------------------------------------------------------</p>'
				+'<p class="mark x25">备注: '+o.mark
				+'<div class="line">-----------------------------------------------------------</div>'
				+'<table class="layui-table2" >'
				+'<tr class="head">'
				+'<td>名称</td>'
				+'<td align="right">数量</td>'
				+'<td align="right">价格</td>'
				+'</tr>'
				+'<tbody>';
				var yuanjia = 0;
				if(o.list){
					for (var i = 0; i < o.list.length; i++) {
						var li = o.list[i];
						ht+='<tr>'
							+'<td >'+(i+1)+'.'+li.goodsTitle+'</td>'
							+'<td align="right">x'+li.goodsNum+'</td>'
							+'<td align="right">'+(li.goodsPrice / 100).toFixed(2)+'</td>'
							+'</tr>';
						yuanjia +=( li.goodsPrice * li.goodsNum);
					}
				}
				ht+='</tbody>'
				+'</table>'
				+'<p class="line">--------------------------------------------------------------------</p>'
				+'<div class="huod x12">';
				if(o.type==1){
					ht+='<span>包装费:</span><p class="line-right">'+(o.packPrice / 100).toFixed(2)+'</p>'
					+'<span>配送费:</span><p class="line-right">'+(o.sendPrice / 100).toFixed(2)+'</p>';
					yuanjia +=  o.sendPrice +  o.packPrice ;
				}else{
					ht+='<span>餐具费:</span><p class="line-right">'+(o.packPrice / 100).toFixed(2)+'</p>';
					yuanjia +=  o.packPrice ;
				}
				//活动
				var activityIds = o.activityIds;
				if (activityIds) {
					var actids = activityIds.split(",");
					for (var i = 0; i < actids.length; i++) {
						var split = actids[i].split("-");
						var pri = split[1];
						var pp = (pri / 100).toFixed(2);
						switch (split[0]) {
						case "1":
							ht+='<span>新客立减:</span><p class="line-right">-'+pp+'</p>';
							break;
						case "2":
							ht+='<span>满减活动:</span><p class="line-right">-'+pp+'</p>';
							break;
						case "3":
							ht+='<span>单品折扣:</span><p class="line-right">-'+pp+'</p>';
							break;
						case "4":
							ht+='<span>堂食满减:</span><p class="line-right">-'+pp+'</p>';
							break;
						case "5":
							ht+='<span>堂食折扣:</span><p class="line-right">-'+pp+'</p>';
							break;
						default:
							break;
						}
					}

				}
				ht+='</div>'
				+'<p class="line">-----------------------------</p>'
				+'<p class="line-right x12">原价: ¥'+(yuanjia/100).toFixed(2)+'</p>'
				+'<p class="line-right x25">实付: ¥'+(o.payPrice/100).toFixed(2)+'</p>';
				if(o.type ==1 ){
					ht+='<p class="line">----------------------------------------------</p>'
					+'<p class="x25">地址:</p>'
					+'<p class="x25">'+o.userName+'</p>'
					+'<p class="x25">'+o.userPhone+'</p>'
					+'<p class="x25">'+o.userAddress+'</p>';
				}
				ht+='</div><div>&nbsp;</div><div>&nbsp;</div><div>&nbsp;</div>';
	$('.print-in-out').html(ht);
	 myAddHtml();
}

//获取提现申请个数 和 入驻申请个数
function getEnterIn(){
	_ajax_post_token('../trans/getShopTranDoing',{},function(res){
		var ds = res.returnObject;
		if(ds.shops!=0){
			var cc = ds.shops;
			if(cc>99){
				cc = "99+";
			}
			$("#nav a[_href='shop-audit.html']").find('span').text(cc).addClass('layui-badge');
			$("#nav a[_href='shop-audit.html']").parent().parent().parent().find("a").eq(0).find('span').addClass('layui-badge').text(cc);
		}
		if(ds.trans!=0){
			var cc = ds.trans;
			if(cc>99){
				cc = "99+";
			}
			$("#nav a[_href='shop-trans-list.html']").find('span').text(cc).addClass('layui-badge');
			$("#nav a[_href='shop-trans-list.html']").parent().parent().parent().find("a").eq(0).find('span').addClass('layui-badge').text(cc);

		}
	});
	
}
